using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Main.Inventory
{

    public class ItemBounce : MonoBehaviour
    {
        [SerializeField] private Transform m_spriteTransform;
        [SerializeField] private BoxCollider2D m_collider;

        [SerializeField] private float m_gravity = 10f;
        [SerializeField] private bool m_isGround = false;
        [SerializeField] private float m_distance;
        [SerializeField] private Vector2 m_direction;
        [SerializeField] private Vector3 m_targetPos;

        private void Update()
        {
            ThrowItem();
        }

        public void SetTarget(Vector3 target, Vector2 direction)
        {
            m_collider.enabled = false;
            m_direction = direction;
            m_targetPos = target;
            m_distance = Vector3.Distance(target, this.transform.position);

            m_spriteTransform.localPosition = Vector3.up * 1.5f;
        }

        private void ThrowItem()
        {
            m_isGround = m_spriteTransform.localPosition.y < 0.1f;

            if (Vector3.Distance(m_targetPos, this.transform.position) > 0.1f)
            {
                transform.position += m_distance * m_gravity * Time.deltaTime * (Vector3)m_direction;
            }
            else
            {
                transform.position = m_targetPos;
            }

            if (!m_isGround)
            {
                m_spriteTransform.position += m_gravity * Time.deltaTime * Vector3.down;
            }
            else
            {
                m_spriteTransform.localPosition = Vector3.zero;
                m_collider.enabled = true;
            }
        }


    }
}